﻿/*
 *Author: Jeff Liu 
 *
 *Under MIT License
 *jebberwocky@gmail.com
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CtpGglTranslate.Interface;
using System.Data.SQLite;
using System.Data;

namespace CtpGglTranslate.Impl
{
    /// <summary>
    /// Caching by Sqlite
    /// @version 0.1
    /// </summary>
    public class SqliteCacheManager : ICacheManger
    {
        #region ICacheManger Members

        public String FileLocation { get; set; }

        public void put(string key, object value)
        {
            using (SQLiteConnection cnn = new SQLiteConnection("Data Source=" + FileLocation))
            {
                cnn.Open();
                using (SQLiteCommand cmd = cnn.CreateCommand())
                {
                    cmd.CommandText = "INSERT INTO cache (key,result) VALUES('"+key+"','"+value+"');";
                    try
                    {
                        cmd.ExecuteNonQuery();
                    }
                    catch (System.Data.SQLite.SQLiteException excep)
                    {
                        if (excep.Message.Contains("column key is not unique"))
                        {
                            cmd.CommandText  = "UPDATE cache SET result ='"+value+"' WHERE key='"+key+"';";
                            cmd.ExecuteNonQuery();
                        }
                        else
                        {
                            throw excep;
                        }
                    }
                }
            }
        }

        public void invalidate(string key)
        {
            using (SQLiteConnection cnn = new SQLiteConnection("Data Source=" + FileLocation))
            {
                cnn.Open();
                using (SQLiteCommand cmd = cnn.CreateCommand())
                {
                    cmd.CommandText = "DELETE FROM cache WHERE key = '"+key+"'";
                    cmd.ExecuteNonQuery();
                }
            }
        }

        public void invalidateAll()
        {
            using (SQLiteConnection cnn = new SQLiteConnection("Data Source=" + FileLocation))
            {
                cnn.Open();
                using (SQLiteCommand cmd = cnn.CreateCommand())
                {
                    cmd.CommandText = "DELETE FROM cache";
                    cmd.ExecuteNonQuery();
                }
            }
        }

        public void put(string key, object value, TimeSpan duration)
        {
            throw new NotImplementedException();
        }

        public object get(string key)
        {
            using (SQLiteConnection cnn = new SQLiteConnection("Data Source=" + FileLocation))
            {
                cnn.Open();
                using (SQLiteCommand cmd = cnn.CreateCommand())
                {
                    cmd.CommandText = "SELECT result FROM cache WHERE key='"+key+"'";
                    IDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        return reader["result"];
                    }
                }
                return null;
            }
        }

        public List<string> getKeys()
        {
            List<string> list = new List<string>();
            using (SQLiteConnection cnn = new SQLiteConnection("Data Source=" + FileLocation))
            {
                cnn.Open();
                using (SQLiteCommand cmd = cnn.CreateCommand())
                {
                    cmd.CommandText = "SELECT key FROM cache";
                    IDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        list.Add(DataUtil.ObjetToString(reader["key"]));
                    }
                }
            }
            return list;
        }

        #endregion
    }
}
